package com.hyh.fireworks.web;


import com.hyh.fireworks.common.result.AjaxResult;
import com.hyh.fireworks.common.result.CommonPage;
import com.hyh.fireworks.common.service.ICommonService;
import com.hyh.fireworks.common.utils.MimeTypeUtil;
import com.hyh.fireworks.entity.Photo;
import com.hyh.fireworks.exception.file.FileMimeTypeException;
import com.hyh.fireworks.service.IPhotoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 相册表 前端控制器
 * </p>
 *
 * @author summerday
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/photo")
public class PhotoController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(PhotoController.class);

    @Resource
    IPhotoService photoService;

    @Resource
    ICommonService commonService;

    @GetMapping("/page")
    @ApiOperation("分页查询图片")
    public AjaxResult pageList(@RequestParam(value = "current", defaultValue = "1") Long current,
                               @RequestParam(value = "pageSize", defaultValue = "5") Long pageSize) {
        CommonPage<Photo> photoCommonPage = photoService.pageList(current, pageSize);
        return AjaxResult.ok(photoCommonPage);
    }

    @GetMapping("/group")
    @ApiOperation("按照组名分页查询图片")
    public AjaxResult listByGroup(@RequestParam(value = "groupName",required = false) String groupName,
                                  @RequestParam(value = "current", defaultValue = "1") Long current,
                                  @RequestParam(value = "pageSize", defaultValue = "5") Long pageSize){
        CommonPage<Photo> photoCommonPage =  photoService.pageListByGroup(groupName, current, pageSize);
        return AjaxResult.ok(photoCommonPage);
    }

    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public AjaxResult upload(@RequestBody MultipartFile file) {
        String url = commonService.upload(file, MimeTypeUtil.IMAGE_EXTENSION);
        return AjaxResult.ok("上传成功!", url);
    }

    @PostMapping("/addPhoto")
    @ApiOperation("添加单张")
    public AjaxResult addOne(@RequestBody Photo photo) {
        boolean save = photoService.save(photo);
        return save ? AjaxResult.ok("保存成功"): AjaxResult.error("保存失败");
    }

    @PostMapping("/addPhotos")
    @ApiOperation("批量添加")
    public AjaxResult addOne(@RequestBody List<Photo> photos) {
        boolean save = photoService.saveBatch(photos);
        return save ? AjaxResult.ok("保存成功"): AjaxResult.error("保存失败");
    }

    @PostMapping("/upload-add")
    @ApiOperation("上传图片并添加")
    public AjaxResult uploadAdd(@RequestBody MultipartFile file) {
        String name = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf('.'));
        String url = commonService.upload(file,MimeTypeUtil.IMAGE_EXTENSION);
        Photo photo = new Photo();
        photo.setTakeTime(LocalDateTime.now());
        photo.setDescription("无");
        photo.setUrl(url);
        photo.setName(name);
        photoService.save(photo);
        return AjaxResult.ok("上传成功!", photo);
    }

}

